﻿using MCPlatform.Communicate.SerialPorts;
using MCPlatform.UI.Components;
using Microsoft.Extensions.Primitives;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace MCPlatform.UI.Controls.ViewModels.Communicate
{
    public class SerialPortDebugViewModel:ViewModelBase
    {
        #region CTOR

        public SerialPortDebugViewModel(ISerialPort serialPortComm)
        {
            SerialPortComm = serialPortComm;
            SerialPortComm.SerialReceivedDataEvent += SerialPortComm_SerialReceivedDataEvent;
            SerialPortComm.SerialReceivedDataEventByHex += SerialPortComm_SerialReceivedDataEventByHex;


        }

        private void SerialPortComm_SerialReceivedDataEventByHex(string obj)
        {
            if (HexDisplayIsChecked)
            {
                stringBuilder.Append(System.DateTime.Now + ":  ");
                stringBuilder.Append(obj);
                stringBuilder.Append(Environment.NewLine);
                ReceiveString = stringBuilder.ToString();
            }
           
        }

        private void SerialPortComm_SerialReceivedDataEvent(string obj)
        {
            if (!HexDisplayIsChecked)
            {
                stringBuilder.Append(System.DateTime.Now + ":  ");
                stringBuilder.Append(obj);
                stringBuilder.Append(Environment.NewLine);
                ReceiveString = stringBuilder.ToString();
            }
          
        }
        #endregion
        #region Properties

        public bool HexDisplayIsChecked 
        { 
            get => _HexDisplayIsChecked;
            set
            { 
                Set(ref _HexDisplayIsChecked, value); 
                
            }
        }
        private bool _HexDisplayIsChecked;

        public bool HexSendIsChecked { get => _HexSendIsChecked; set => Set(ref _HexSendIsChecked, value); }
        private bool _HexSendIsChecked;

        public string? SendString { get => _SendString; set => Set(ref _SendString, value); }
        private string? _SendString;

       
        public string? ReceiveString { get => _ReceiveString; 
            set => Set(ref _ReceiveString, value); }
        private string? _ReceiveString;

        private StringBuilder stringBuilder = new StringBuilder();


        public ISerialPort? SerialPortComm { get => _SerialPortComm; set => Set(ref _SerialPortComm, value); }
        private ISerialPort? _SerialPortComm;

        #endregion

        #region Methods

        public void ClearSend()
        {
            SendString = string.Empty;
        }

        public void ClearReceive()
        {
            ReceiveString = string.Empty;
            stringBuilder.Clear();
        }

        public  void Send()
        {
           if (SerialPortComm is null || SendString is null) 
            {
                Toast.Show("发送信息为空！",targetWindow:ToastWindow.MainWindow);
                return; 
            }

            if (HexSendIsChecked)
            {
                SerialPortComm.SendDataByHex(SendString);
            }
            else
            {
                SerialPortComm.SendData(SendString);
            }



        }

        #endregion
    }
}
